package com.erclab.internal.xpresso.lang;

import java.util.HashMap;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;

import org.apache.log4j.Logger;

import com.erclab.internal.xpresso.Application;
import com.erclab.internal.xpresso.conf.Settings;

public  abstract class LanguageHandler {

	// Create the log for this class
	private static final Logger log = Logger.getLogger(Application.class);
	
	protected HashMap languages;
	
	public LanguageHandler() throws LanguageException{
		log.info("Creating a Language Handler");
		String supLangs = Settings.getProperty("application.language.supported.languages");
		if (supLangs == null || supLangs.equals("")){
			log.info("No language support for this application");
		} else {
			log.info("Supported languages: " + supLangs);
			String[] langs = supLangs.split(",");
			languages = new HashMap();
			for (int i =0; i < langs.length; i++){
				log.info("Loading language properties for " + "application.language." + langs[i]);
				String propsPath = Settings.getProperty("application.language." + langs[i]);
				PropertyResourceBundle properties = (PropertyResourceBundle)ResourceBundle.getBundle(propsPath);
				languages.put(langs[i], properties);
				log.info(langs[i] + " loaded");
			}
		}
	}
	
	public String getLabel(String language, String label) throws LanguageException{
		String thelabel = "";
		if (label == null){
			return "";
		}
		if (this.languages == null || language == null){
			return label;
		}
		try{
			thelabel = ((PropertyResourceBundle)languages.get(language)).getString(label);
		} catch (java.util.MissingResourceException e){
			return "Label: " + label + " not found !";
		}
		return thelabel;
	}
	
	public abstract String getLanguage(Object aGenericObject);
	
}
